import axios from 'axios'
import ls from '@/utils/Storage'
import { VERSION } from '@/store/mutation-types'
let updating = false
const isNewVersion = () => {
  if (!updating) {
    const url = `/version.json?t=${new Date().getTime()}`
    updating = true
    axios
      .get(url, {
        headers: {
          'Content-Type': 'application/json'
        }
      })
      .then((res) => {
        updating = false
        if (res.status === 200) {
          const vueVersion = res.data.version
          const localVueVersion = ls.get(VERSION)
          if (localVueVersion && localVueVersion != vueVersion) {
            ls.set(VERSION, vueVersion)
            window.location.reload()
            return
          } else {
            ls.set(VERSION, vueVersion)
          }
        }
      })
      .catch((e) => {
        updating = false
      })
  }
}

export default {
  isNewVersion
}
